package DAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Hibernate.HibernateUtil;
import Negocio.Cliente;
import Negocio.ClienteParticular;
import Negocio.Empresa;
import Negocio.Proveedor;


public class HibernateClienteDAO {
	private static HibernateClienteDAO instancia = null;
	private static SessionFactory sf = null;
	
	
	public static HibernateClienteDAO getInstancia(){
		if(instancia == null){
			instancia = new HibernateClienteDAO();
			sf = HibernateUtil.getSessionFactory();
		}
		return instancia;
	}
	
	
	
	public void grabarCliente(Cliente cliente){
		try
		{
			Session session = sf.openSession();
			session.beginTransaction();
			session.persist(cliente);
			session.flush();
			session.getTransaction().commit();
			session.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
			System.out.println("Error al grabar un Cliente");
		}
	}
	
	public void updateCliente(Cliente cliente){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(cliente);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch (Exception e){
			System.out.println(e);
            System.out.println("Error al actualizar el Cliente");
		}
		
	}

	
	public ClienteParticular getClienteParticular(int doc){
        try {
        	Session session=sf.openSession();
            session.beginTransaction();
            ClienteParticular cliente = (ClienteParticular)session.createQuery("from ClienteParticular cli where cli.dni = ?").setInteger(0, doc).uniqueResult();
            session.getTransaction().commit();
            session.close();
            return cliente;
        } catch (Exception e){
            System.out.println(e);
            System.out.println("Error al cargar el Cliente Particular");
        }
        return null;
	}

	public Cliente getClienteEmpresa(long cuit) 
	{
        try 
        {
        	Session session=sf.openSession();
            session.beginTransaction();
            Empresa empresa = (Empresa)session.createQuery("from Empresa emp where emp.cuit = ?").setLong(0, cuit).uniqueResult();
            session.getTransaction().commit();
            session.close();
            return empresa;
        }
        catch (Exception e)
        {
            System.out.println(e);
            System.out.println("Error al cargar una Empresa");
        }
        return null;
	}



	public int getUltimoID() {
		int ultimoID = 0;
		try {
        	Session session=sf.openSession();
            session.beginTransaction();
            Query q = session.createQuery("select max(e.idCliente) from Cliente as e");
            if (q.uniqueResult() == null)
            		ultimoID = 0;
            else
            	ultimoID =  (int) q.uniqueResult();
            System.out.println("DAOCLIENTE El ultimo ID en BD es " + ultimoID);
            session.getTransaction().commit();
            session.close();
	    }
		catch (Exception e){
		        System.out.println(e);
		        System.out.println("Error al cargar el Cliente");
		}
		return ultimoID;
	 }



	public Cliente getCliente(int id) {
		try {
        	Session session=sf.openSession();
            session.beginTransaction();
            Cliente cliente = (Cliente)session.createQuery("from Cliente cli where cli.idCliente = ?").setInteger(0,id).uniqueResult();
            session.close();
            return cliente;
        } catch (Exception e){
            System.out.println(e);
            System.out.println("Error al cargar un Cliente");
        }
        return null;
	}

}
